package com.zjitc.service.impl;

import com.alipay.api.domain.OrderItem;
import com.zjitc.dao.CartDao;
import com.zjitc.dao.OrderDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.model.Orderitem;
import com.zjitc.model.Orders;
import com.zjitc.model.Product;
import com.zjitc.service.OrderService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/22 15:21
 * @description:
 */
@Service
public class OrderServiceImpl implements OrderService{

  @Autowired OrderDao orderDao;
  @Autowired CartDao cartDao;
  @Autowired ProductDao productDao;
  @Override
  public void add(Orders orders) {
    orderDao.add(orders);
    cartDao.clearCart();
  }

  @Override
  public void updateOrder(String oid, String name, String phone, String addr) {
    orderDao.updateOrder(oid,name,phone,addr);
  }

  @Override
  public List<Orders> findPageByUid(Integer uid) {

    List<Orders> orderList = orderDao.findPageByUid(uid);
    for (Orders item: orderList) {
      List<Orderitem> orderItem = orderDao.findOrderItems(item.getOid());
      for (Orderitem item1:orderItem){
        Product product = productDao.findProductById(item1.getPid());
        item1.setProduct(product);
      }
      item.setOrderitemList(orderItem);
    }
    return orderList;
  }

  @Override
  public void deleteOrder(String oid) {
    orderDao.deleteOrder(oid);
  }

  @Override
  public List<Orders> findPageAllOrders() {
    return orderDao.findPageAllOrders();
  }

  @Override
  public void updateOrderState(String oid) {
    orderDao.updateOrderState(oid);
  }
}
